package com.bestvike.stone.spring.feign;

import feign.Contract;
import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * Created by 许崇雷 on 2018/7/5.
 */
@Component
@ConditionalOnClass(FeignContext.class)
public class FeignClientUtils {
    private static Feign.Builder feignBuilder;

    FeignClientUtils(FeignClientsConfiguration configuration, Contract contract) {
        feignBuilder = configuration.feignBuilder(null).contract(contract);
    }

    public static <T> T build(Class<T> clazz, String url) {
        Assert.notNull(clazz, "clazz can not be null.");
        Assert.hasLength(url, "url can not be empty.");
        return feignBuilder.target(clazz, url.startsWith("http") ? url : "http://" + url);
    }
}
